package com.google.code.swingonfire.component.calendar;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;

import com.google.code.swingonfire.component.calendar.JYearTableModel.MonthEntry;

public class JYearTableMonthRenderer implements TableCellRenderer
{

    private final JButton button;
    private final Color defaultForeground;
    private final Font defaultFont;

    public JYearTableMonthRenderer()
    {
        super();

        button = new JButton();

        prepare(button);

        defaultForeground = button.getForeground();
        defaultFont = button.getFont();
    }

    protected void prepare(JButton button)
    {
        button.setHorizontalAlignment(SwingConstants.CENTER);
        button.setMargin(new Insets(1, 2, 1, 2));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column)
    {
        MonthEntry entry = (MonthEntry) value;

        button.setText(entry.getMonthName());
        button.setContentAreaFilled(hasFocus || isSelected);
        button.setBorderPainted(hasFocus || isSelected);

        Color foreground = entry.getMonthForeground(defaultForeground);

        button.setForeground((foreground != null) ? foreground : defaultForeground);

        Font font = entry.getMonthFont(defaultFont);

        button.setFont((font != null) ? font : defaultFont);
        button.setOpaque(false);

        return button;
    }

}
